package smartfleet.mobile.util.parser;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import smartfleet.mobile.location.Location;
import smartfleet.mobile.station.Station;
import smartfleet.mobile.vehicle.Vehicle;

public class SmartFleetParser {

	static String response = "1,Aeroporto Portela,off,1,Aeroporto Portela,38.771149,-9.128523";
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Station station = toStation(response);
		System.out.println(station);
	}

	public static Station toStation(String response){
		
		Station station = new Station();
		
		if(response.length()!=0){
			
			StringTokenizer tokenizer = new StringTokenizer(response, ",",false);
			String id = tokenizer.nextToken();
			String state = tokenizer.nextToken();
			
			station = new Station(id, state);
			
			String idLocation = tokenizer.nextToken();
			String locationName = tokenizer.nextToken();
			String locationLatidude = tokenizer.nextToken();
			String locationLongitude = tokenizer.nextToken();
			
			Location location = new Location(idLocation,locationName,locationLatidude,locationLongitude);
			station.setLocation(location);
			
		}
		
		return station;
	}
	
	public static List<Station> getOnlineStations(String response){
		
		List<Station> list = new ArrayList<Station>();
		
		StringTokenizer tokenizer =  new StringTokenizer(response,";",false);
		
		while(tokenizer.hasMoreTokens()){
			String token = tokenizer.nextToken();
			Station station = toStation(token);
			list.add(station);
		}
		
		return list;
	}
	
	
	public static Location toLocation(String response){
		
		Location location = new Location();
		
		if(response.length()!=0){
			StringTokenizer tokenizer = new StringTokenizer(response, ",",false);
			
			String idLocation = tokenizer.nextToken();
			String locationName = tokenizer.nextToken();
			String locationLatidude = tokenizer.nextToken();
			String locationLongitude = tokenizer.nextToken();
			
			location = new Location(idLocation,locationName,locationLatidude,locationLongitude);
		}
		
		return location;
	}
	
	public static Vehicle toVehicle(String response){
		Vehicle vehicle = new Vehicle();
		
		if(response.length()!=0){
			
			StringTokenizer tokenizerVeiculoData = new StringTokenizer(response, ";",false);
			String dadosVeiculo =  tokenizerVeiculoData.nextToken();
			String dadosCurrentLocation = tokenizerVeiculoData.nextToken();
			
			StringTokenizer tokenizer = new StringTokenizer(dadosVeiculo, ",",false);
			
			String id = tokenizer.nextToken();
			String state = tokenizer.nextToken();
			String batteryLevel = tokenizer.nextToken();
			String seatsAvailable = tokenizer.nextToken();
			String currentAltitude = tokenizer.nextToken();
			
			Location location = toLocation(dadosCurrentLocation);
			
			vehicle = new Vehicle(id, batteryLevel, state, seatsAvailable,currentAltitude);
			vehicle.setCurrentLocation(location);
		}
		
		return vehicle;
	}
	
	
	
	
}
